from PyQt5 import QtCore, QtGui, QtWidgets
import record
import extract
import analysis
import player

class Mainwindows(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setObjectName("IR_AnalysisSoftware")
        self.resize(711, 396)
        self.gridLayout_5 = QtWidgets.QGridLayout(self)
        self.gridLayout_5.setContentsMargins(11, 11, 11, 11)
        self.gridLayout_5.setSpacing(6)
        self.gridLayout_5.setObjectName("gridLayout_5")
        spacerItem = QtWidgets.QSpacerItem(20, 92, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_5.addItem(spacerItem, 0, 1, 1, 1)
        spacerItem1 = QtWidgets.QSpacerItem(63, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_5.addItem(spacerItem1, 1, 0, 1, 1)
        self.gridLayout_3 = QtWidgets.QGridLayout()
        self.gridLayout_3.setSpacing(6)
        self.gridLayout_3.setObjectName("gridLayout_3")
        spacerItem2 = QtWidgets.QSpacerItem(80, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_3.addItem(spacerItem2, 0, 1, 1, 1)
        self.pushButton_6 = QtWidgets.QPushButton(self)
        self.pushButton_6.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_6.setObjectName("pushButton_6")
        self.gridLayout_3.addWidget(self.pushButton_6, 0, 5, 1, 1)
        self.pushButton_8 = QtWidgets.QPushButton(self)
        self.pushButton_8.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_8.setObjectName("pushButton_8")
        self.gridLayout_3.addWidget(self.pushButton_8, 2, 3, 1, 1)
        spacerItem3 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_3.addItem(spacerItem3, 1, 3, 1, 1)
        spacerItem4 = QtWidgets.QSpacerItem(80, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_3.addItem(spacerItem4, 2, 1, 1, 1)
        spacerItem5 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_3.addItem(spacerItem5, 1, 5, 1, 1)
        self.pushButton_5 = QtWidgets.QPushButton(self)
        self.pushButton_5.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_5.setObjectName("pushButton_5")
        self.gridLayout_3.addWidget(self.pushButton_5, 0, 3, 1, 1)
        spacerItem6 = QtWidgets.QSpacerItem(80, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_3.addItem(spacerItem6, 0, 4, 1, 1)
        spacerItem7 = QtWidgets.QSpacerItem(80, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_3.addItem(spacerItem7, 2, 4, 1, 1)
        self.pushButton_9 = QtWidgets.QPushButton(self)
        self.pushButton_9.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_9.setObjectName("pushButton_9")
        self.gridLayout_3.addWidget(self.pushButton_9, 2, 5, 1, 1)
        self.pushButton_4 = QtWidgets.QPushButton(self)
        self.pushButton_4.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_4.setObjectName("pushButton_4")
        self.gridLayout_3.addWidget(self.pushButton_4, 0, 0, 1, 1)
        self.pushButton_7 = QtWidgets.QPushButton(self)
        self.pushButton_7.setMinimumSize(QtCore.QSize(121, 61))
        self.pushButton_7.setObjectName("pushButton_7")
        self.gridLayout_3.addWidget(self.pushButton_7, 2, 0, 1, 1)
        spacerItem8 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_3.addItem(spacerItem8, 1, 0, 1, 1)
        self.gridLayout_5.addLayout(self.gridLayout_3, 1, 1, 1, 1)
        spacerItem9 = QtWidgets.QSpacerItem(63, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout_5.addItem(spacerItem9, 1, 2, 1, 1)
        spacerItem10 = QtWidgets.QSpacerItem(20, 92, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.gridLayout_5.addItem(spacerItem10, 2, 1, 1, 1)

        self.retranslateUi()

    def retranslateUi(self):
        self.setWindowTitle("IR_AnalysisSoftware")
        self.pushButton_4.setText("Record rawdata")
        self.pushButton_4.clicked.connect(self.open_Record_rawdata)
        self.pushButton_5.setText("Extract images")
        self.pushButton_5.clicked.connect(self.open_Extract_images)
        self.pushButton_6.setText("Analysis image")
        self.pushButton_6.clicked.connect(self.open_Analysis_image)
        self.pushButton_7.setText("Play file.data")
        self.pushButton_7.clicked.connect(self.open_Play_file_data)
        self.pushButton_8.setText("RES")
        self.pushButton_9.setText("RES")
        # 保存对子窗口的引用
        self.subwindow = []
        for i in range(6):
            self.subwindow.append(None)

        self.page2 = extract.Extract()

    def open_Record_rawdata(self):
        # 如果子窗口已经存在，先关闭它
        if self.subwindow[0] is not None:
            self.subwindow[0].close()
        # 创建并显示子窗口
        self.subwindow[0] = record.Record()
        self.subwindow[0].show()

    def open_Extract_images(self):
        # 如果子窗口已经存在，先关闭它
        if self.subwindow[1] is not None:
            self.subwindow[1].close()
        # 创建并显示子窗口
        self.subwindow[1] = self.page2
        self.subwindow[1].show()

    def open_Analysis_image(self):
        # 如果子窗口已经存在，先关闭它
        if self.subwindow[2] is not None:
            self.subwindow[2].close()

        # 创建并显示子窗口
        self.subwindow[2] = analysis.Analysis(self.page2.image)
        self.subwindow[2].show()

    def open_Play_file_data(self):
        # 如果子窗口已经存在，先关闭它
        if self.subwindow[3] is not None:
            self.subwindow[3].close()

        # 创建并显示子窗口
        self.subwindow[3] = player.Player()
        self.subwindow[3].show()

    def closeEvent(self, event):
        # 在主窗口关闭时，确保子窗口也关闭
        for i in range(6):
            if self.subwindow[i] is not None:
                self.subwindow[i].close()
                self.subwindow[i] = None
        event.accept()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainwin = Mainwindows()
    mainwin.show()
    sys.exit(app.exec_())
